package com.example.libraryapp.activity.borrowHistory;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.libraryapp.Constant;
import com.example.libraryapp.R;
import com.example.libraryapp.activity.bookDetail.BookDetailActivity;
import com.example.libraryapp.adapter.BookAdapter;
import com.example.libraryapp.adapter.BorrowHistoryAdapter;
import com.example.libraryapp.adapter.base.BaseAdapter;
import com.example.libraryapp.bean.Book;
import com.example.libraryapp.bean.BookBorrow;
import com.example.libraryapp.bean.User;
import com.example.libraryapp.bean.response.BaseResponse;
import com.example.libraryapp.network.base.BaseActivity;
import com.example.libraryapp.util.LoginUtil;
import com.example.libraryapp.widget.CnToolbar;

import java.util.List;

/**
 * @author 伍凯铭
 * @since 2020/6/28
 */
public class BorrowHistoryActivity extends BaseActivity<BorrowHistoryContract.View, BorrowHistoryPresentIml>
        implements View.OnClickListener, BorrowHistoryContract.View, BaseAdapter.OnItemClickListener{

    private User mUser;

    private BorrowHistoryAdapter mBorrowHistoryAdapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mUser = LoginUtil.getNowUser(this);
        if(mUser == null) {
            finish();
        }
    }

    @Override
    protected void onStart() {
        super.onStart();
        showLoading();
        mPresenter.getBorrowHistory(mUser.getUserId());
    }

    @Override
    public void onClick(View v) {
        finish();
    }

    @Override
    public void showBorrowHistory(BaseResponse<List<BookBorrow>> response) {
        if(response.getResult() != null && !response.getResult().isEmpty()) {
            mBorrowHistoryAdapter.refreshData(response.getResult());
        }else {
            showEmpty();
        }
    }

    @Override
    public void onItemClick(View view, int position) {
        Book book = mBorrowHistoryAdapter.getItem(position).getBook();
        Intent intent = new Intent(BorrowHistoryActivity.this, BookDetailActivity.class);
        intent.putExtra(Constant.BOOK_ID,book.getBookId());
        startActivity(intent);
    }

    @Override
    protected void initView(View view) {
        RecyclerView recyclerView = view.findViewById(R.id.recycler_view);
        mBorrowHistoryAdapter = new BorrowHistoryAdapter(this);
        mBorrowHistoryAdapter.setOnItemClickListener(this);
        recyclerView.setLayoutManager(new LinearLayoutManager(this,RecyclerView.VERTICAL,false));
        recyclerView.setAdapter(mBorrowHistoryAdapter);
        recyclerView.addItemDecoration(new DividerItemDecoration(this,DividerItemDecoration.VERTICAL));
    }

    @Override
    protected void initToolBar(CnToolbar toolbar) {
        toolbar.setTitle(getResources().getString(R.string.borrow_history));
        toolbar.setBackBtnOnClickListener(this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_borrow_history;
    }

    @Override
    protected BorrowHistoryPresentIml initPresenter() {
        return new BorrowHistoryPresentIml();
    }
}
